package Thread;

import java.util.Scanner;

//volatile,可以很好的禁止优化,能够保证每次都是从内存上读取数据.
public class Test23 {
    //测试内存可见性,下面加了volatile关键字,此时输入一个非0的数才会结束线程
    volatile public static int flag = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (flag == 0){

            }
            System.out.println("t线程结束");
        });
        Thread t2 = new Thread(()->{
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个整数:");
            flag = sc.nextInt();

        });
        t.start();
        t2.start();
    }

    static int count = 0;
    public static void main1(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0;i<5000;i++){
                count++;
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0;i<5000;i++){
                count++;
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(1000);
        System.out.println(count);
    }
}
